package com.primaryedu.encrpy;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.text.TextUtils;


/**
 * 加密文件
 * @description 
 * @data 2016年8月4日

 * @author jun.wang
 */
public class EncrpyUtils {
	
	public static String getPhone(String phone){
		if(TextUtils.isEmpty(phone)){
			return "";
		}
		
	    AES gAes = new AES();  
      
        String sendString= phone;  
        byte[] sendBytes = null;  
        try {  
            sendBytes = sendString .getBytes("UTF8");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
      
        String str = gAes.encrypt(sendBytes);  
        String result = gAes.decrypt(str);  
        
        System.out.println("strresult==" + str);
        System.out.println("strresult==" + result);
		
		return str;	
	}
	
	public static String getId(String phone){
		if(TextUtils.isEmpty(phone)){
			return "";
		}
		
	    AES gAes = new AES();  
      
        String sendString= phone;  
        byte[] sendBytes = null;  
        try {  
            sendBytes = sendString .getBytes("UTF8");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
      
        String str = gAes.encrypt(sendBytes);  
        String result = gAes.decrypt(str);  
        
        System.out.println("strresult==" + str);
        System.out.println("strresult==" + result);
		
		return str;	
	}
	
	public static String getPhoneDecode(String phone){
		if(TextUtils.isEmpty(phone)){
			return "";
		}
		
	    AES gAes = new AES();  
      
        String str = gAes.decrypt(phone);  
		
        System.out.println("strresult=getPhoneDecode=" + str);
        
		return str;	
		
//		return phone ;
	}
	
	public static String getMd5(String string){
		byte[] hash;
	    try {
	        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
	    } catch (NoSuchAlgorithmException e) {
	        throw new RuntimeException("Huh, MD5 should be supported?", e);
	    } catch (UnsupportedEncodingException e) {
	        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
	    }

	    StringBuilder hex = new StringBuilder(hash.length * 2);
	    for (byte b : hash) {
	        if ((b & 0xFF) < 0x10) hex.append("0");
	        hex.append(Integer.toHexString(b & 0xFF));
	    }
	    return hex.toString();	
	}

}
